package com.emily.test.controller;

import com.alibaba.fastjson.JSONObject;
import com.emily.test.controller.result.ResponseResult;
import com.emily.test.model.OSlideshow;
import com.emily.test.service.RedisService;
import com.emily.test.service.SlideshowService;
import com.emily.test.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ：zc
 * @date ：Created in 2019/4/2 10:24
 * @description：
 */
@Slf4j
@RestController
@RequestMapping(value = "slideshow")
@Api(value = "首页轮播图", description = "轮播图相关接口", position = 1, produces = MediaType.APPLICATION_JSON_VALUE)
public class SlideshowController {
    @Autowired
    private  SlideshowService slideshowService;
    @Autowired
    private RedisService redisService;
    @ApiOperation(value="获取轮播图列表", notes="根据公司code获取轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyCode", value = "companyCode", required = true)
    })
    @PostMapping(value = "selectSlideshowListByCode")
    public ResponseResult selectSlideshowListByCode(HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String,Object> userMap= (Map<String, Object>) redisService.get(token);
        String code= userMap.get("companyCode") == null ? null : (String)userMap.get("companyCode");
        try {
            List<OSlideshow> list =  slideshowService.selectList(code);
            log.info("获取SlideshowList={}",list);
            return ResponseResult.success("查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("查询失败");
        }
    }
    @ApiOperation(value="官网轮播图列表", notes="根据公司code获取官网轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyCode", value = "companyCode", required = true)
    })
    @PostMapping(value = "getSlideshowList")
    public ResponseResult getSlideshowList(@RequestBody(required = false) JSONObject jsonObject){
        String code =jsonObject.getString("companyCode");
        try {
            List<OSlideshow> list =  slideshowService.selectList(code);
            log.info("获取PartnerList={}",list);
            return ResponseResult.success("成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("查询失败");
        }
    }
    @ApiOperation(value="增加一个轮播图", notes="增加轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "imgPath", value = "图片地址", required = true),
            @ApiImplicitParam(name = "id", value = "轮播id", required = true),
    })
    @PostMapping(value = "insertSlideshow")
    public ResponseResult insertSlideshow(@RequestBody JSONObject jsonObject,HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String,Object> userMap= (Map<String, Object>) redisService.get(token);
        String code= userMap.get("companyCode") == null ? null : (String)userMap.get("companyCode");
        String imgPath = jsonObject.getString("imgPath");
        String id = jsonObject.getString("id");
        try {
            if(id !=null && id.length()!=0 && !id.equals("")){
                log.info("修改旧轮播图");
                OSlideshow oSlideshow = slideshowService.selectOSlideshow(Integer.parseInt(id));
                oSlideshow.setImgPath(imgPath);
                oSlideshow.setCompanyCode(code);
                oSlideshow.setUpdateTime(DateUtil.getNowStr());
                slideshowService.updateNews(oSlideshow);
            }else {
                log.info("新建轮播图");
                OSlideshow o = new OSlideshow();
                o.setImgPath(imgPath);
                o.setCompanyCode(code);
                o.setUpdateTime(DateUtil.getNowStr());
                slideshowService.insert(o);
            }

            log.info("增加成功");
            return ResponseResult.success("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("增加失败");
        }
    }
    @ApiOperation(value="删除轮播图", notes="删除一个或多个轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "轮播图id集合格式[1,2,3]", required = true),
    })
    @PostMapping(value = "delSlideshow")
    public ResponseResult delSlideshow(@RequestBody JSONObject jsonObject){
        String[] ids = jsonObject.getString("ids").replaceAll("\\[", "").replaceAll("]", "").split(",");
        try {
            for (int i = 0; i < ids.length; i++) {
                slideshowService.deleteByIds(Integer.parseInt(ids[i]));
            }
            log.info("删除轮播图成功");
            return ResponseResult.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("删除失败");
        }
    }
    @ApiOperation(value="修改一个轮播图", notes="修改轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "轮播图id", required = true),
            @ApiImplicitParam(name = "imgPath", value = "图片地址", required = true),
            @ApiImplicitParam(name = "companyCode", value = "公司code", required = true),
    })
    @PostMapping(value = "updateSlideshow")
    public ResponseResult updateSlideshow(@RequestBody JSONObject jsonObject){
        String code = jsonObject.getString("companyCode");
        String imgPath = jsonObject.getString("imgPath");
        Integer id = jsonObject.getInteger("id");
        try {
            OSlideshow oSlideshow = slideshowService.selectOSlideshow(id);
            oSlideshow.setImgPath(imgPath);
            oSlideshow.setCompanyCode(code);
            oSlideshow.setUpdateTime(new Date(System.currentTimeMillis()).toString());
            int ss  = slideshowService.insert(oSlideshow);
            log.info("修改成功}",oSlideshow);
            return ResponseResult.success(oSlideshow);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("修改失败");
        }
    }
}
